﻿#define MySql
#undef MSSql
 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;

using System.IO;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Net.Mail;

namespace ukeasbjWeb
{
    public partial class EmployeeEdit : Bonus 
    {
        //电子邮件
        string sEmail;

        /// <summary>
        /// 功能
        /// NewOne, 
        /// EditEmployee,
        /// ReadOnly,
        /// </summary>
        public enum Func
        {
            EditEmployee,
            NewOne,
            ReadOnly,
        }
        //		public Func eFunctions;

        protected void Page_Load(object sender, EventArgs e)
        {
            /*  测试c#后台代码，关闭浏览器
            Thread.Sleep(1000);
            Response.Write("<script>window.close();</script>");
            */ 

            // 操作权限的判断
			Common_SXD.Is_LongIn(Response, Session, Request);

			bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
			bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_HR = Convert.ToBoolean(Session["Is_HR"]);
            bool bIs_Marketing = Convert.ToBoolean(Session["Is_Marketing"]);
                     
            ///地区经理、系统管理员、人力资源可以进入员工信息页面
            if (!bIs_O_M && !bIs_Admin && !bIs_HR)
			{
				Response.Write("<h2 align='center'><font face='宋体' color='red'>");
				Response.Write("<br>对不起，您不能使用本功能。" + 
					"<br>请用其它身份登录。");
				Response.Write("</fong></h2>");
				Response.End();
		
			}
			
			///////////// 读出电子邮件
			sEmail = Request["Email"];  
			///////////// 分离加在电子邮件前部的控制字符
			Regex EditEmployee = new Regex("([0-9a-zA-Z_?]{1,})");
			Regex NewOne = new Regex("(NewOne)");

            if (!IsPostBack)
            {
                Common_SXD.Init_Office(DDList_Office);
 
                if (NewOne.IsMatch(sEmail))
                {
                    Init_NewOne(); //初始化：添加新员工
                    //			eFunctions = Func.NewOne;

                }
                else if (EditEmployee.IsMatch(sEmail))
                {// 初始化：修改第一次付款数据
                    //					eFunctions = Func.EditEmployee;
                    Init_Edit_Employee();
                }
            }

            Label_Msg.Text = sEmail;
        }

        /// <summary>
        ///  初始化：只读合同数据
        /// </summary>
        private void Init_ReadOnly()
        {
        }

        /// <summary>
        /// 初始化，添加新的合同数据
        /// </summary>
        public void Init_NewOne()
        {
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            if (bIs_Admin)
            {
                DDList_Office.Enabled = true;
                CheckBox_Is_MOTP.Enabled = true; // 默认为 MOTP 密码可修改
            }
            else
            {
                DDList_Office.Enabled = false;
                CheckBox_Is_MOTP.Enabled = false; // 默认为 MOTP 密码不可修改
            }

            CheckBox_Is_O_M.Enabled = false;
            CheckBox_Is_G_M.Enabled = false;
            CheckBox_Is_Collector.Enabled = false;
            CheckBox_Is_Cashier.Enabled = false;
            CheckBox_Is_Receptionist.Enabled = false;
            CheckBox_Is_Admin.Enabled = false;
            CheckBox_Is_Accountant.Enabled = false;
            CheckBox_Is_Active.Enabled = false; // 默认为活跃数据
            CheckBox_Is_Active.Checked = true;
            CheckBox_Changed_PW.Enabled = false;
            CheckBox_Is_MOTP.Enabled = true; // 默认为 MOTP 密码
            CheckBox_Is_HR.Enabled = false;
            CheckBox_Is_Frontline_Agent.Enabled = false;
            
            CheckBox_Is_Frontline_Agent.Enabled = false;

            /// 各地办公室，添加自己的数据
            /// 手机型号信息在修改时加入
            /// 
            Common_SXD.SetSelectedIndex(Session["Office"].ToString().Trim(), DDList_Office);
            /// 北京、上海办公室不需要手机型号信息
            ///
            if (DDList_Office.SelectedItem.Text.Trim() == "北京" || DDList_Office.SelectedItem.Text.Trim() == "上海")
            {
                TextBox_Model_Mobile.Enabled = false;
            }
            else
            {
                TextBox_Model_Mobile.Enabled = true;
            }

            Response.Write("<li>新员工数据。</li>");
        }

        /// <summary>
        /// 不能添加（更新）重复的“初始化数字串”
        /// </summary>
        private string Not_Repeat_InitNumber(string sSql_input, string strSQL, string strFilter, HttpResponse myResponse)
        {
            ////////////////////////
            ///不允许重复的“初始化数字串”
            ////////////////////////
#if MySql
            string sCounter = Common_SXD.CHAR_MySQL_PLUS1(strSQL, strFilter, myResponse);
#elif MSSql
            string sCounter = Common_SXD.CHAR_MSSQL_PLUS1(strSQL, strFilter, myResponse);
#endif
            if (Convert.ToInt16(sCounter) > 1)
            {
                Response.Write("<li>已经存在" + Convert.ToString((Convert.ToInt16(sCounter) - 1)).Trim() + "个相同的‘初始化数字串’无法更新！</li>");
                return sSql_input;
            }
            else
            {
                Response.Clear();
                return sSql_input += " InitNumber = '" + TextBox_InitNumber.Text.Trim() + "', ";
            }

        }
        /// <summary>
        /// 存放读入的员工信息，以备更新时核对。
        /// </summary>
//	    private static string sInitNumber;  /* 初始化数字串 */


        /// <summary>
        /// 读取员工信息的初始值
        /// </summary>
        public void Read_Init_Employee()
        {
            ///SqlConnection sqlCnn = new SqlConnection(strCnn);
            ///
            // 根据传入的合同编号，生成查询 SQL 语句
            string sSql = "select * from Employees where Email = " + "'" + sEmail + "'";

#if MySql
            DataSet bsDataSet = Common_SXD.Init_MySql_DataSet("Employees", sSql);
#elif MSSql
            DataSet bsDataSet = Common_SXD.Init_MSSql_DataSet("Employees", sSql);
#endif
            if (bsDataSet.Tables["Employees"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["Employees"].Rows[0]; // 只有一行
                //				// 显示员工信息
                TextBox_Email.Text = rowUser["Email"].ToString().Trim();

                TextBox_Code_Employee.Text = rowUser["Code_Employee"].ToString().Trim();

                TextBox_Model_Mobile.Text = rowUser["Model_Mobile"].ToString().Trim();

                TextBox_C_N_Employee.Text = rowUser["C_N_Employee"].ToString().Trim();

                TextBox_Pin.Text = rowUser["Pin"].ToString().Trim();

                TextBox_InitNumber.Text = rowUser["InitNumber"].ToString().Trim();

                TextBox_E_N_Employee.Text = rowUser["E_N_Employee"].ToString().Trim();

                TextBox_Suffix_No_Contract.Text = rowUser["Suffix_No_Contract"].ToString().Trim();

                if (TextBox_Date_Entry.Visible)
                {
                    TextBox_Date_Entry.Text = rowUser["Date_Entry"].ToString().Trim();
                    TextBox_Date_Entry.Text = Common_SXD.TimeToYMD(TextBox_Date_Entry.Text);
                }
                if (TextBox_Date_Departure.Visible)
                {
                    TextBox_Date_Departure.Text = rowUser["Date_Departure"].ToString().Trim();
                    TextBox_Date_Departure.Text = Common_SXD.TimeToYMD(TextBox_Date_Departure.Text);
                }

                Common_SXD.SetSelectedIndex(rowUser["Office"].ToString().Trim(), DDList_Office);

                /// 北京、上海办公室不需要手机型号信息
                ///
                /*if (DDList_Office.SelectedItem.Text.Trim() == "北京" || DDList_Office.SelectedItem.Text.Trim() == "上海")
                {
                    TextBox_Model_Mobile.Enabled = false;
                }
                else
                {
                    TextBox_Model_Mobile.Enabled = true;
                }*/


                if (rowUser["Is_Counsellor"].ToString().Trim() == "1" || rowUser["Is_Counsellor"].ToString().Trim() == "True")
                    CheckBox_Is_Counsellor.Checked = true;
                else
                    CheckBox_Is_Counsellor.Checked = false;
                /////////////////////////////////////////////////////
                if (rowUser["Is_Writer"].ToString().Trim() == "1")
                    CheckBox_Is_Writer.Checked = true;
                else
                    CheckBox_Is_Writer.Checked = false;
                /////////////////////////////////////////////////////

                if (rowUser["Is_O_M"].ToString().Trim() == "1")
                    CheckBox_Is_O_M.Checked = true;
                else
                    CheckBox_Is_O_M.Checked = false;

                if (rowUser["Is_G_M"].ToString().Trim() == "1")
                    CheckBox_Is_G_M.Checked = true;
                else
                    CheckBox_Is_G_M.Checked = false;


                if (rowUser["Is_Collector"].ToString().Trim() == "1")
                    CheckBox_Is_Collector.Checked = true;
                else
                    CheckBox_Is_Collector.Checked = false;

                if (rowUser["Is_Cashier"].ToString().Trim() == "1")
                    CheckBox_Is_Cashier.Checked = true;
                else
                    CheckBox_Is_Cashier.Checked = false;

                if (rowUser["Is_Receptionist"].ToString().Trim() == "1")
                    CheckBox_Is_Receptionist.Checked = true;
                else
                    CheckBox_Is_Receptionist.Checked = false;

                if (rowUser["Is_Admin"].ToString().Trim() == "1")
                    CheckBox_Is_Admin.Checked = true;
                else
                    CheckBox_Is_Admin.Checked = false;

                if (rowUser["Is_Accountant"].ToString().Trim() == "1")
                    CheckBox_Is_Accountant.Checked = true;
                else
                    CheckBox_Is_Accountant.Checked = false;

                if (rowUser["Is_Active"].ToString().Trim() == "1")
                    CheckBox_Is_Active.Checked = true;
                else
                    CheckBox_Is_Active.Checked = false;

                if (rowUser["Is_HR"].ToString().Trim() == "1")
                    CheckBox_Is_HR.Checked = true;
                else
                    CheckBox_Is_HR.Checked = false;

                if (rowUser["Is_Marketing"].ToString().Trim() == "1")
                    CheckBox_Is_Marketing.Checked = true;
                else
                    CheckBox_Is_Marketing.Checked = false;

                if (rowUser["Is_Frontline_Agent"].ToString().Trim() == "1")
                    CheckBox_Is_Frontline_Agent.Checked = true;
                else
                    CheckBox_Is_Frontline_Agent.Checked = false;

                if (rowUser["Changed_PW"].ToString().Trim() == "1")
                    CheckBox_Changed_PW.Checked = true;
                else
                    CheckBox_Changed_PW.Checked = false;

                if (rowUser["Is_MOTP"].ToString().Trim() == "1")
                    CheckBox_Is_MOTP.Checked = true;
                else
                    CheckBox_Is_MOTP.Checked = false;

            }
            

        }
        

        /// <summary>
        /// 更新员工信息，不是新数据了
        /// </summary>
        public void Update_Employee()
        {
            /*
            SqlConnection sqlCnn = new SqlConnection();
            sqlCnn.ConnectionString = strCnn;
            sqlCnn.Open(); // 建立连接
            SqlCommand sqlCmd = sqlCnn.CreateCommand(); // 创建 command 对象
            sqlCmd.CommandType = CommandType.Text; // 类型为 SQL 语句
            */
            /*
            MySqlConnection MySqlconn = new MySqlConnection();
            MySqlconn.ConnectionString = MySqlstrCnn;
            MySqlconn.Open(); // 建立连接
            MySqlCommand MysqlCmd = MySqlconn.CreateCommand(); // 创建 command 对象
            MysqlCmd.CommandType = CommandType.Text; // 类型为 SQL 语句
            */
            //合同信息写入数据库
            string sSql = "Update Employees Set";

            if (TextBox_Model_Mobile.Enabled)
            {
                sSql += " Model_Mobile  = '" + TextBox_Model_Mobile.Text.Trim() + "',";

            }
            ///if (TextBox_C_N_Employee.Enabled)
            ///{
            ///    sSql += " C_N_Employee = '" + TextBox_C_N_Employee.Text.Trim() + "',";
            ///}
            sSql = sSql_Add_TextBox(TextBox_C_N_Employee, "C_N_Employee", sSql);

            if (TextBox_E_N_Employee.Enabled)
            {
                sSql += " E_N_Employee = '" + TextBox_E_N_Employee.Text.Trim() + "', ";
            }
            if (TextBox_Pin.Enabled)
            {
                sSql += " Pin = '" + TextBox_Pin.Text.Trim() + "', ";
            }
            if (TextBox_InitNumber.Enabled)
            {
                sSql = Not_Repeat_InitNumber(sSql, "select count(Email) from Employees ",
                    "InitNumber like '" + TextBox_InitNumber.Text.Trim() + "'", Response);
            }
            if (TextBox_Code_Employee.Enabled)
            {
                sSql += " Code_Employee = '" + TextBox_Code_Employee.Text.Trim() + "', ";
            }
            if (TextBox_Suffix_No_Contract.Enabled)
            {
                sSql += " Suffix_No_Contract = '" + TextBox_Suffix_No_Contract.Text.Trim() + "', ";
            }
            if (DDList_Office.SelectedItem.Enabled)
            {
                sSql += " Office = '" + DDList_Office.SelectedItem.Text.Trim() + "', ";
            }
            if (TextBox_Date_Entry.Enabled)
            {
             //   sSql += " Date_Entry = '" + TextBox_Date_Entry.Text.Trim() + "', ";
            }
            if (TextBox_Date_Departure.Enabled)
            {
               // sSql += " Date_Departure = '" + TextBox_Date_Departure.Text.Trim() + "', ";
            }

            string temp;

            if (CheckBox_Is_Counsellor.Enabled)
            {
                if (CheckBox_Is_Counsellor.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Counsellor = " + temp + ", ";
            }
            /*if (CheckBox_Is_O_M.Enabled)
            //{
                if (CheckBox_Is_O_M.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_O_M = " + temp + ", ";
            }*/
            sSql = sSql_Add_Checkd(CheckBox_Is_O_M, "Is_O_M", sSql);
            sSql = sSql_Add_Checkd(CheckBox_Is_G_M, "Is_G_M", sSql);
            if (CheckBox_Is_Collector.Enabled)
            {
                if (CheckBox_Is_Collector.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Collector = " + temp + ", ";
            }
            if (CheckBox_Is_Cashier.Enabled)
            {
                if (CheckBox_Is_Cashier.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Cashier = " + temp + ", ";
            }
            if (CheckBox_Is_Receptionist.Enabled)
            {
                if (CheckBox_Is_Receptionist.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Receptionist = " + temp + ", ";
            }
            if (CheckBox_Is_Admin.Enabled)
            {
                if (CheckBox_Is_Admin.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Admin = " + temp + ", ";
            }
            if (CheckBox_Is_Accountant.Enabled)
            {
                if (CheckBox_Is_Accountant.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Accountant = " + temp + ", ";
            }
            if (CheckBox_Is_Active.Enabled)
            {
                if (CheckBox_Is_Active.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Active = " + temp + ", ";
            }
            if (CheckBox_Is_Frontline_Agent.Enabled)
            {
                if (CheckBox_Is_Frontline_Agent.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Frontline_Agent = " + temp + ", ";
            }
            if (CheckBox_Is_HR.Enabled)
            {
                if (CheckBox_Is_HR.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_HR = " + temp + ", ";
            }
            if (CheckBox_Is_Marketing.Enabled)
            {
                if (CheckBox_Is_Marketing.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_Marketing = " + temp + ", ";
            }
            if (CheckBox_Changed_PW.Enabled)
            {
                if (CheckBox_Changed_PW.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Changed_PW = " + temp + ", ";
            }

            if (CheckBox_Is_MOTP.Enabled)
            {
                if (CheckBox_Is_MOTP.Checked == true)
                    temp = "1";
                else
                    temp = "0";
                sSql += " Is_MOTP = " + temp + ", ";
            }

            /////////////////////////////////////////////////////////
            if (CheckBox_Is_Writer.Checked == true)
                temp = "1";
            else
                temp = "0";
            sSql += " Is_Writer = " + temp + " ";

            sSql += " where Email = " + "'" + Label_Msg.Text.Trim() + "'";
#if MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif
            
        }

        /// <summary>
        /// 关闭全部的界面控件
        /// </summary>
        private void false_Enable_All_Controls()
        {
            Bt_OK.Enabled = false;
            DDList_Office.Enabled = false;

            CheckBox_Is_Counsellor.Enabled = false;
            CheckBox_Is_Writer.Enabled = false;

            CheckBox_Is_Collector.Enabled = false;
            CheckBox_Is_O_M.Enabled = false;
            CheckBox_Is_G_M.Enabled = false;
            CheckBox_Is_Cashier.Enabled = false;
            CheckBox_Is_Receptionist.Enabled = false;
            CheckBox_Is_Admin.Enabled = false;
            CheckBox_Is_Accountant.Enabled = false;
            CheckBox_Is_Active.Enabled = false; // 默认为活跃数据
            CheckBox_Changed_PW.Enabled = false;
            CheckBox_Is_MOTP.Enabled = false; // 默认为 MOTP 密码

            TextBox_InitNumber.Visible = false;
            TextBox_InitNumber.Enabled = false;
            TextBox_C_N_Employee.Enabled = false;
            Label_Initsecret.Visible = false;

            CheckBox_Is_HR.Enabled = false;
            CheckBox_Is_Frontline_Agent.Enabled = false;

            TextBox_Date_Departure.Enabled = false;
            TextBox_Date_Entry.Enabled = false;

            TextBox_Email.Enabled = false;
            TextBox_Pin.Enabled = false;
            TextBox_Pin.Visible = false;

            Button_NewPassword.Enabled = false;
            Button_OTP_Initializing.Enabled = false;

        }


        /// <summary>
        /// 添加新员工信息
        /// </summary>
        public void Insert_Employee()
        {
            //不合理数据

            ////////////////////////
            ///不允许重复的“初始化数字串”
            ////////////////////////
            string sCounter = "0";
            if (TextBox_InitNumber.Text.Trim() != "")
            {
                ///Common_SXD.CHAR_MySQL_PLUS1(strSQL, strFilter, myResponse);
#if MySql
                sCounter = Common_SXD.CHAR_MySQL_PLUS1("select count(Email) from Employees ",
                    "InitNumber like '" + TextBox_InitNumber.Text.Trim() + "'", Response);
#elif MSSql
                sCounter = Common_SXD.CHAR_MSSQL_PLUS1("select count(Email) from Employees ",
                    "InitNumber like '" + TextBox_InitNumber.Text.Trim() + "'", Response);
#endif
            }
            if (Convert.ToInt16(sCounter) > 1)
            {
                Response.Write("<li>已经存在" + Convert.ToString((Convert.ToInt16(sCounter) - 1)).Trim() + "个‘" + TextBox_InitNumber.Text.Trim() + "’初始化数字串！无法添加！</li>");
                return;
            }
            else
                Response.Clear();
            /*
            SqlConnection sqlCnn = new SqlConnection();
            sqlCnn.ConnectionString = strCnn;
            sqlCnn.Open(); // 建立连接
            SqlCommand sqlCmd = sqlCnn.CreateCommand(); // 创建 command 对象
            sqlCmd.CommandType = CommandType.Text; // 类型为 SQL 语句
            */
            /*
            MySqlConnection MySqlconn = new MySqlConnection();
            MySqlconn.ConnectionString = MySqlstrCnn;
            MySqlconn.Open(); // 建立连接
            MySqlCommand MysqlCmd = MySqlconn.CreateCommand(); // 创建 command 对象
            MysqlCmd.CommandType = CommandType.Text; // 类型为 SQL 语句
            */
            //合同信息写入数据库
            string sSql = "insert into Employees  (C_N_Employee, Pin, E_N_Employee, Code_Employee, Suffix_No_Contract,Office, Email, Password, Is_Counsellor, Is_G_M, Is_O_M, Is_Collector, Is_Cashier, Is_Receptionist, Is_Admin, Is_Accountant, Is_Active, Is_HR, Is_Marketing, Is_Frontline_Agent, Changed_PW, Is_Writer )";
            sSql += "values(";
            sSql += "'" + TextBox_C_N_Employee.Text.Trim() + "',"; 
            sSql += "'" + TextBox_Pin.Text.Trim() + "', ";
            sSql += "'" + TextBox_E_N_Employee.Text.Trim() + "', ";
            sSql += " '" + TextBox_Code_Employee.Text.Trim() + "', ";
            sSql += " '" + TextBox_Suffix_No_Contract.Text.Trim() + "', ";
            sSql += " '" + DDList_Office.SelectedItem.Text.Trim() + "', ";
            sSql += " '" + TextBox_Email.Text.Trim() + "',";
            sSql += " '12345678',";

            string temp;

            if (CheckBox_Is_Counsellor.Checked == true)
                temp = "1";
            else
                temp = "0";
            sSql += " " + temp + ", ";

            sSql += " 0, ";
            sSql += " 0, ";
            sSql += " 0, ";
            sSql += " 0, ";
            sSql += " 0, ";
            sSql += " 0, ";
            sSql += " 0, ";
            sSql += " 1, ";//活跃数据
            sSql += " 0, ";
            sSql += " 0, ";
            sSql += " 0, ";
            sSql += " 0, ";
            
            if (CheckBox_Is_Writer.Checked == true)
                temp = "1";
            else
                temp = "0";
            sSql += " " + temp + " ";
            sSql += " )";
#if MySql
            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#elif MSSql
            Common_SXD.MSSql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);
#endif
            
        }

        /// <summary>
        ///  初始化：第一次付款的新数据
        /// </summary>
        private void Init_Edit_Employee()
        {
            TextBox_Email.Enabled = false;
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            /// 系统管理管理员，有全部权限
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            /// 各地O M 的权限
            bool bIs_HR = Convert.ToBoolean(Session["Is_HR"]);
            /// 人力资源的权限、
            bool bIs_Marketing = Convert.ToBoolean(Session["Is_Marketing"]);
            /// 市场专员的权限、
            /// 
            false_Enable_All_Controls();

            if (bIs_Admin)
            {
                Bt_OK.Enabled = true;
                DDList_Office.Enabled = true;
                CheckBox_Is_Counsellor.Enabled = true;
                CheckBox_Is_Writer.Enabled = true;
                CheckBox_Is_Collector.Enabled = true;

                CheckBox_Is_O_M.Enabled = true;
                CheckBox_Is_G_M.Enabled = true;
                CheckBox_Is_Cashier.Enabled = true;
                CheckBox_Is_Receptionist.Enabled = true;
                CheckBox_Is_Admin.Enabled = true;
                CheckBox_Is_Accountant.Enabled = true;
                CheckBox_Is_Active.Enabled = true; // 默认为活跃数据
                CheckBox_Changed_PW.Enabled = true;
                CheckBox_Is_MOTP.Enabled = true; // 默认为 MOTP 密码

                TextBox_InitNumber.Visible = true;
                TextBox_InitNumber.Enabled = true;
                TextBox_C_N_Employee.Enabled = true;
                Label_Initsecret.Visible = true;

                CheckBox_Is_HR.Enabled = true;
                CheckBox_Is_Frontline_Agent.Enabled = true;

                TextBox_Pin.Enabled = true;
                TextBox_Pin.Visible = true;

                Button_NewPassword.Enabled = true;
                Button_OTP_Initializing.Enabled = true;

                TextBox_Date_Departure.Enabled = true;
                TextBox_Date_Entry.Enabled = true;

            }
            else if (bIs_O_M)
            {
                Bt_OK.Enabled = true;
                DDList_Office.Enabled = false;

                CheckBox_Is_Counsellor.Enabled = true;
                CheckBox_Is_Writer.Enabled = true;

                CheckBox_Is_Receptionist.Enabled = true;
                CheckBox_Is_Frontline_Agent.Enabled = true;

                CheckBox_Is_Active.Enabled = true;

                TextBox_Pin.Enabled = true;
                TextBox_Pin.Visible = true;

                Button_NewPassword.Enabled = true;

 
            }
            else if (bIs_HR)
            {
                Bt_OK.Enabled = true;
                /// 人事专员
                CheckBox_Is_Frontline_Agent.Enabled = true;
                CheckBox_Is_Active.Enabled = true;
                TextBox_Date_Departure.Enabled = true;
                TextBox_Date_Entry.Enabled = true;

            }
            else
            {

            }

            
            Read_Init_Employee();

            Response.Write("<li>修改员工数据。</li>");

            string sInitsecret = Common_SXD.Make_Initsecret(TextBox_InitNumber.Text.ToString().Trim());
            if (sInitsecret != "")
                Label_Initsecret.Text = "Init-Secret:" + sInitsecret;
        }

        protected void Bt_OK_Click(object sender, EventArgs e)
        {
            //string temp = Label_Msg.Text.ToString().Trim();
            //eFunctions = Get_Function(Label_Msg.Text.ToString().Trim());
            switch (Get_Function(Label_Msg.Text.ToString().Trim()))
            {

                case Func.NewOne:
                    Insert_Employee();
                    break;

                case Func.EditEmployee:
                    Update_Employee();
                    break;

                default:
                    break;

            }

        }

        /// <summary>
        /// 读取功能标识
        /// </summary>
        private Func Get_Function(string strFunction)
        {
            Regex EditEmployee = new Regex("([0-9a-zA-Z_]{2,})");
            Regex NewOne = new Regex("(NewOne)");

            if (NewOne.IsMatch(strFunction))
            {
                return Func.NewOne;
            }
            else if (EditEmployee.IsMatch(strFunction))
            {
                return Func.EditEmployee;
            }
            return Func.EditEmployee;
        }

        protected void Button_NewPassword_Click(object sender, EventArgs e)
        {
           /// 生成新的固定密码
           /// 更新数据库中保存的密码
            Common_SXD.Update_FixedPassword(Common_SXD.Make_FixedPassword(Response), Label_Msg.Text.Trim(), Response);
            Thread.Sleep(100);
           /// 从数据库中读取密码，发送到用户邮箱 
            Common_SXD.Sendmail_Password(TextBox_Email.Text.Trim(),/*
                DDList_Office.SelectedItem.Text.Trim(), TextBox_C_N_Employee.Text.Trim(),*/ Response);
        }

        /// <summary>
        /// 发送OTP初始化邮件到员工的信箱
        /// </summary>
        private void Sendmail_OTP_Initial()
        {
            ///string strOffice = DDList_Office.SelectedItem.Text.Trim(); 
            ///string strCounsellor_Writer = TextBox_C_N_Employee.Text.ToString().Trim();
            Password myPass = new Password();
            ///读取密码
            myPass.read(TextBox_Email.Text.ToString().Trim());

            if ( !myPass.bIs_MOTP || myPass.sEmail == "" || myPass.sInitNumber.Length != 25)
            {
                Response.Clear();
                Response.Write("<li>不能用OTP密码方式登录,不发送相关邮件！</li>");
                return;
            }

            try
            {
                // 服务器名，端口号
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(Common_SXD.smtp_ukeas, 25);
                smtp.UseDefaultCredentials = false;
                //账户，密码
                smtp.Credentials = new System.Net.NetworkCredential("jianwei_rong@promisingEDU.com", Common_SXD.pwd_jianwei_rong);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage myMsg = new System.Net.Mail.MailMessage();
                //发件人
                myMsg.From = new MailAddress("jianwei_rong@promisingEDU.com", "戎建伟", Encoding.GetEncoding("GB2312"));

                myMsg.SubjectEncoding = Encoding.GetEncoding("GB2312");
                myMsg.BodyEncoding = Encoding.GetEncoding("GB2312");
                myMsg.Subject = myPass.sOffice + " " + myPass.sC_N_Emoloyee + "OTP初始化";
                /// 邮件的主体
                myMsg.Body = "Hi, " + myPass.sEmail + "!\r\n";
                myMsg.Body += "\r\n";

                myMsg.Body += "请注意，您的 '25 random keys' 是" + myPass.sInitNumber + "。\r\n"; 
                myMsg.Body += "\r\n";

                myMsg.Body += "如果您的手机支持java程序，请解压缩本邮件的附件，将“Mobile-OTP 1.07版”程序(MobileOTP.jar)复制到你的手机里。一般的做法是用手机的电缆连接计算机与手机，然后复制。附件里还有一张MobileOTP程序的启动画面，仅供参考无需拷贝到手机里。\r\n";
                myMsg.Body += "\r\n";

                myMsg.Body += "在手机上，请按照下面的步骤完成OTP程序的初始化:\r\n";
                myMsg.Body += "1) 启动程序“Mobile-OTP 1.07版”；\r\n";
                myMsg.Body += "2) 输入四个零，“0000”，然后输入本封邮件中给出的“25 random keys”，该程序会立即显示“Init-Secret”码；\r\n";
                myMsg.Body += "3) 如果本次初始化产生的“Init-Secret”码与下一封邮件给出的编码一致，说明初始化成功。否则需要重复1)至2)的操作，注意25个数字不要输错。\r\n";
                myMsg.Body += "\r\n";

                myMsg.Body += "初始化完成之后，每次登录GreenDB数据库需要在手机上，启动程序“Mobile-OTP 1.07版”，输入4位数字的pin码，手机会生成的6位密码(Pass), 用这些密码登陆数据库（每次操作生成密码是不一样的）。如果您的手机时间与与标准时间一致（误差不要超过正负1分钟），服务会算出相同的密码，然后允许您登录数据库系统。\r\n";
                myMsg.Body += "\r\n";

                myMsg.Body += "您的pin码是4位数字，一般由各地办公室的经理设置。pin码不能是4个‘0’，程序认为“0000”是初始化命令。\r\n";
                myMsg.Body += "\r\n";

                myMsg.Body += "登录GreenDB数据库的操作：\r\n";
                myMsg.Body += "1) 启动程序“Mobile-OTP 1.07版”, 输入4位的pin码;\r\n";
                myMsg.Body += "2) 迅速按照手机显示的6位密码(Pass), 登录数据库。\r\n";
                myMsg.Body += "如果登录失败，请注意核对pin码。重复1)至2)的操作。\r\n";
                myMsg.Body += "\r\n";

                myMsg.Body += "注：请保持手机时间与标准时间一致（误差不要超过正负1分钟），这样GreenDB服务器才能算出相同的密码，允许您登录。\r\n";
                myMsg.Body += "\r\n";

                myMsg.Body += "“Mobile-OTP 1.07版”程序是一个开放源代码的程序，可以在 http://motp.sourceforge.net/ 网站上下载源程序与相应的运行程序。\r\n";
                myMsg.Body += "\r\n";
                
                myMsg.Body += "戎建伟\r\n";



                string strEmail = myPass.sEmail;

                string sAddress_Email = Common_SXD.eMail_Replacement(strEmail); ///strEmail + "@promisingEDU.com";
                //收件人
                myMsg.To.Add(new MailAddress(sAddress_Email, "", Encoding.GetEncoding("GB2312")));
                //密送
                //myMsg.Bcc.Add(new MailAddress("rongjianwei@sina.com", "", Encoding.GetEncoding("GB2312")));

                Response.Charset = "gb2312";

                //读取当前目录的path
                //				string strFilePath = Directory.GetCurrentDirectory();
                //读取服务器物理目录
                string strFilePath = Request.PhysicalApplicationPath;

                //Common_SXD.Set_BonusDate(TextBox_Year_Month.Text, false);

                string file = strFilePath + @"..\.\html\V107.rar";

                //添加附件
                System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(file);
                myMsg.Attachments.Add(attachment);
                myMsg.Priority = System.Net.Mail.MailPriority.Low;
                smtp.Send(myMsg);
                myMsg.Attachments.Remove(attachment);
                attachment.Dispose();

                Response.Clear();
                Response.Write("<li>OTP初始化邮件已经发送！</li>");
                //               MessageBox MyB = new MessageBox(this);
                //               MyB.Show("奖金表已经发送！");

            }
            catch (System.Net.Mail.SmtpException se)
            {
                Response.Write("<li>smtp error.</li>" + se.Message);
            }
            catch (FormatException se)
            {
                Response.Write("<li>format error.</li>" + se.Message);
            }
            catch (System.Exception e)
            {
                Response.Write("<li>system error.</li>" + e.Message + "; Target: " + e.TargetSite);
            }


        }

        /// <summary>
        /// 发送OTP程序的初始化密匙到员工的信箱
        /// </summary>
        private void Sendmail_Init_Secret()
        {
            //string strOffice = DDList_Office.SelectedItem.Text.Trim();
            //string strCounsellor_Writer = TextBox_C_N_Employee.Text.ToString().Trim();

            Password myPass = new Password();
            ///读取密码
            myPass.read(TextBox_Email.Text.ToString().Trim() );

            if (!myPass.bIs_MOTP || myPass.sEmail == "" || myPass.sInitNumber.Length != 25)
            {
                Response.Clear();
                Response.Write("<li>不能用OTP密码方式登录,不发送相关邮件！</li>");
                return;
            }

            try
            {
                // 服务器名，端口号
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(Common_SXD.smtp_ukeas, 25);
                smtp.UseDefaultCredentials = false;
                //账户，密码
                smtp.Credentials = new System.Net.NetworkCredential("jianwei_rong@promisingEDU.com", Common_SXD.pwd_jianwei_rong);
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                System.Net.Mail.MailMessage myMsg = new System.Net.Mail.MailMessage();
                //发件人
                myMsg.From = new MailAddress("jianwei_rong@promisingEDU.com", "戎建伟", Encoding.GetEncoding("GB2312"));

                myMsg.SubjectEncoding = Encoding.GetEncoding("GB2312");
                myMsg.BodyEncoding = Encoding.GetEncoding("GB2312");
                myMsg.Subject = myPass.sOffice + " " + myPass.sC_N_Emoloyee + " Init Secret ";


                /// 邮件的主体
                myMsg.Body = "Hi, " + myPass.sEmail + "!\r\n";
                myMsg.Body += "\r\n";

                string sInitsecret = Common_SXD.Make_Initsecret(myPass.sInitNumber);
                if (sInitsecret != "")
                     myMsg.Body += "请注意，您的 'Init-Secret' 是" + sInitsecret + "。OTP程序的初始化的结果必须与它完全一致。\r\n";
                else
                    myMsg.Body += "请注意，您的 'Init-Secret' 出错。\r\n";   
                myMsg.Body += "\r\n";

                myMsg.Body += "戎建伟\r\n";


                string strEmail = myPass.sEmail;

                string sAddress_Email = Common_SXD.eMail_Replacement(strEmail); ///strEmail + "@promisingEDU.com";
                //收件人
                myMsg.To.Add(new MailAddress(sAddress_Email, "", Encoding.GetEncoding("GB2312")));
                //密送
                ///myMsg.Bcc.Add(new MailAddress("rongjianwei@sina.com", "", Encoding.GetEncoding("GB2312")));

                Response.Charset = "gb2312";

                myMsg.Priority = System.Net.Mail.MailPriority.Low;
                smtp.Send(myMsg);
                
                Response.Clear();
                Response.Write("<li>Init Secret 邮件已经发送！</li>");
                //               MessageBox MyB = new MessageBox(this);
                //               MyB.Show("Init Secret 邮件已经发送！");

            }
            catch (System.Net.Mail.SmtpException se)
            {
                Response.Write("<li>smtp error.</li>" + se.Message);
            }
            catch (FormatException se)
            {
                Response.Write("<li>format error.</li>" + se.Message);
            }
            catch (System.Exception e)
            {
                Response.Write("<li>system error.</li>" + e.Message + "; Target: " + e.TargetSite);
            }


        }

        protected void Button_OTP_Initializing_Click(object sender, EventArgs e)
        {
            Init_Edit_Employee();
            Thread.Sleep(100);
            Sendmail_OTP_Initial();
            Thread.Sleep(100);
            Sendmail_Init_Secret();

        }

    }
}
